import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '@/views/login/index.vue'
import Dashboard from '@/views/dashboard/index.vue'
import store from '@/store'

Vue.use(VueRouter)

/**
 * @type {import('vue-router').RouteConfig[]} // 路由配置提示
 */
const routes = [
  {
    path: '/login',
    component: Login
  },
  {
    path: '/',
    component: Dashboard
  }
]

const router = new VueRouter({
  routes
})
router.beforeEach(async (to, from, next) => {
  const token = store.state.user.token
  if (token) {
    if (!store.state.user.id) {
      await store.dispatch('user/getMenuAction')
    }
    if (to.path === '/login') {
      next('/')
    } else {
      next()
    }
  } else {
    next('/login')
  }
})

export default router
